package mil.af.amc.adpetracker.model;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * Represents a single "LOGIN" Account (as opposited to an ADPE account, DRA, etc)
 *
 * @author TSgt Jason Ferguson
 */

@Entity
@Table(name="T_ACCOUNT")
public class Account implements UserDetails, Comparable<Account>, Serializable {

    private int id;
    private Set<Authority> accountAuthorities = new TreeSet<Authority>();
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;

    String username;
    String password;

    String firstName;
    String lastName;
    String middleInitial;
    Rank rank;
    String organization;

    Set<AccountAdpeAccount> accountAdpeAccounts = new HashSet<AccountAdpeAccount>();

    @Id
    @Column(name="ACCOUNT_ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name="T_ACCOUNT_AUTHORITY2",
        joinColumns = @JoinColumn(name="ACCOUNTID"),
            inverseJoinColumns = @JoinColumn(name="AUTHORITYID")
        )
    public Set<Authority> getAccountAuthorities() {
        return accountAuthorities;
    }

    public void setAccountAuthorities(Set<Authority> accountAuthorities) {
        this.accountAuthorities = accountAuthorities;
    }

    @Transient
    public Collection<GrantedAuthority> getAuthorities() {
        return new HashSet<GrantedAuthority>(accountAuthorities);
    }

//    @Transient
//    public GrantedAuthority[] getAuthorities() {
//        return this.accountAuthorities.toArray(new Authority[this.accountAuthorities.size()] );
//    }

    @Column(name="EXPIRED")
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    @Column(name="LOCKED")
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    @Transient
    public boolean isCredentialsNonExpired() {
        return true;
        //return credentialsNonExpired;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    @Column(name="ENABLED")
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Column(name="login")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name="HASHEDPASSWORD")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name="FIRSTNAME")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name="LASTNAME")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name="MI")
    public String getMiddleInitial() {
        return middleInitial;
    }

    public void setMiddleInitial(String middleInitial) {
        this.middleInitial = middleInitial;
    }

    @ManyToOne
    @JoinColumn(name="RANK")
    public Rank getRank() {
        return rank;
    }

    public void setRank(Rank rank) {
        this.rank = rank;
    }

    @Transient
    public String getDisplayName() {
        return lastName + ", " + firstName + " " + middleInitial + " " + rank.getAbbreviation();
    }

    @Column(name="ORGANIZATION")
    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    @OneToMany(mappedBy="account", fetch = FetchType.EAGER)
    public Set<AccountAdpeAccount> getAccountAdpeAccounts() {
        return accountAdpeAccounts;
    }

    public void setAccountAdpeAccounts(Set<AccountAdpeAccount> accountAdpeAccounts) {
        this.accountAdpeAccounts = accountAdpeAccounts;
    }

    public int compareTo(Account o) {

        return (this.getDisplayName().compareTo(o.getDisplayName()));
    }
}
